<?php


include '../test_upload_pic.php';
session_start();
ob_start();
date_default_timezone_set('PRC');
//var_dump($_POST);exit;
$error='';
$title = $_POST['title'];
$price = $_POST['price'];
$days = $_POST['days'];
$area_type = $_POST['area_type'];
$price_type = $_POST['price_type'];
$startingPoint = $_POST['startingPoint'];
$vehicle = $_POST['vehicle'];
$head_pic = "";
$particular_pic = "";
$introduce = $_POST['introduce'];
$schedule = $_POST['schedule'];
$hint = $_POST['hint'];




if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    //判断是否有上传文件
    if (is_uploaded_file($_FILES['head_pic']['tmp_name'])) {
        $upfile = $_FILES['head_pic'];
        print_r($upfile);
        $name = $upfile['name'];    //文件名
        $type = $upfile['type']; //文件类型
        $size = $upfile['size']; //文件大小
        $tmp_name = $upfile['tmp_name'];  //临时文件
        $error = $upfile['error']; //出错原因
        if ($max_file_size < $size) { //判断文件的大小
            echo '上传文件太大';
            exit ();
        }
        if (!in_array($type, $uptypes)) {        //判断文件的类型
            echo '上传文件类型不符' . $type;
            exit ();
        }
        if (!file_exists($destination_folder)) {
            mkdir($destination_folder);
        }
        if (file_exists("upload/" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        }
        $pinfo = pathinfo($name);
        $ftype = $pinfo['extension'];
        $destination = $destination_folder . time().$size . "." . $ftype;
        if (file_exists($destination) && $overwrite != true) {
            echo "同名的文件已经存在了";
            exit ();
        }
        if (!move_uploaded_file($tmp_name, $destination)) {
            echo "移动文件出错";
            exit ();
        }
        $pinfo = pathinfo($destination);
        $fname = $pinfo[basename];
        echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>" . $destination_folder . $fname . "</font><br>";
        echo " 宽度:" . $image_size[0];
        echo " 长度:" . $image_size[1];
        echo "<br> 大小:" . $file["size"] . " bytes";
        
    }
}


$head_pic = $fname;

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    //判断是否有上传文件
    if (is_uploaded_file($_FILES['particular_pic']['tmp_name'])) {
        $upfile = $_FILES['particular_pic'];
        print_r($upfile);
        $name = $upfile['name'];    //文件名
        $type = $upfile['type']; //文件类型
        $size = $upfile['size']; //文件大小
        $tmp_name = $upfile['tmp_name'];  //临时文件
        $error = $upfile['error']; //出错原因
        if ($max_file_size < $size) { //判断文件的大小
            echo '上传文件太大';
            exit ();
        }
        if (!in_array($type, $uptypes)) {        //判断文件的类型
            echo '上传文件类型不符' . $type;
            exit ();
        }
        if (!file_exists($destination_folder)) {
            mkdir($destination_folder);
        }
        if (file_exists("upload/" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
            echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
        }
        $pinfo = pathinfo($name);
        $ftype = $pinfo['extension'];
        $destination = $destination_folder .time() .$size. "." . $ftype;
        if (file_exists($destination) && $overwrite != true) {
            echo "同名的文件已经存在了";
            exit ();
        }
        if (!move_uploaded_file($tmp_name, $destination)) {
            echo "移动文件出错";
            exit ();
        }
        $pinfo = pathinfo($destination);
        $fname = $pinfo[basename];
        echo " <font color=red>已经成功上传</font><br>文件名: <font color=blue>" . $destination_folder . $fname . "</font><br>";
        echo " 宽度:" . $image_size[0];
        echo " 长度:" . $image_size[1];
        echo "<br> 大小:" . $file["size"] . " bytes";
        
    }
}

$particular_pic = $fname;
	//将内容插入到数据库中
	if(empty($error)){
		include '../config.php';
		$link=mysql_connect(HOST,DBUSER,DBPASS) or die('数据库连接失败');
		mysql_select_db(DBNAME,$link);
		mysql_set_charset('utf8');
		$sql= "SELECT * FROM t_plan WHERE title='".$title."'";
		$result=mysql_query($sql);
		if(mysql_num_rows($result)>0){
			$error='您要输入的行程已存在！请做修改';
		}
		mysql_free_result($result);
		if(empty($error)){
			$sql= "INSERT INTO t_plan (title,price,days,area_type,price_type,createdate,startingpoint,vehicle,head_pic,particular_pic,introduce,schedule,hint)
			VALUES('".$title."','".$price."','".$days."','".$area_type."','".$price_type."','".date('Y-m-d')."','"
			.$startingPoint."','".$vehicle."','".$head_pic."','".$particular_pic."','".$introduce."','".$schedule."','".$hint."')";
			$result=mysql_query($sql);
			$tid=mysql_insert_id();
			if($tid<=0){
				$error=$sql.$tid.'插入数据库失败!'.$two_type.$one_type;
			}
		}
		mysql_close($link);
	}
	//错误跳转
	if(empty($error)){
		header('Location:planList.php?tid='.$tid.'&title='.$title);
	}else{
		header('Location:planAdd.php?error='.$error.'title='.$title.'begin='.$begin);
	}
	ob_end_flush();
	?>